<?php 
if($edit) {
    $cs=Yii::app()->getClientScript();
    $cs->registerCoreScript('jquery');
    $cs->registerScriptFile(SystemAsset::toUrl('jquery.jeditable.js'));
    $cs->registerScriptFile(SystemAsset::toUrl('jquery.charcounter.js'));
    $cs->registerScriptFile(SystemAsset::toUrl('jquery.jeditable.charcounter.js'));
    $cs->registerScriptFile(SystemAsset::toUrl('jquery.colorbox.js'));
    $cs->registerCssFile(SystemAsset::toUrl('colorbox.css'));
    $savingImage=CHtml::normalizeUrl(SystemAsset::toUrl('ajax-loader.gif'));
    $indicator=CJavaScript::quote(CHtml::image($savingImage));
    
    $setTitleUrl=CHtml::normalizeUrl(array('setTitle','id'=>$recipe->id));
    $titleMaxLen=RECIPE::TITLE_MAX_LEN;
    $cs->registerScript('setTitle', 
        "$('#recipe-title').editable('$setTitleUrl', {
            indicator : '$indicator',
            type      : 'text_charcounter',
            charcounter : {
                characters : $titleMaxLen
            }            
        });"
    );
    
    $setDescriptionUrl=CHtml::normalizeUrl(array('setDescription','id'=>$recipe->id));
    $descriptionMaxLen=RECIPE::DESCRIPTION_MAX_LEN;
    $cs->registerScript('setDescription', 
        "$('#recipe-description').editable('$setDescriptionUrl', {
            indicator : '$indicator',
            type      : 'textarea_charcounter',
            rows      : 3,
            charcounter : {
                characters : $descriptionMaxLen
            }            
        });"
    );

    $setEffectivenessUrl=CHtml::normalizeUrl(array('setEffectiveness','id'=>$recipe->id));
    $effectivenessMaxLen=RECIPE::EFFECTIVENESS_MAX_LEN;
    $cs->registerScript('setEffectiveness', 
        "$('#recipe-effectiveness').editable('$setEffectivenessUrl', {
            indicator : '$indicator',
            type      : 'textarea_charcounter',
            rows      : 2,
            charcounter : {
                characters : $effectivenessMaxLen
            }            
        });"
    );
    
    $setTipUrl=CHtml::normalizeUrl(array('setTip','id'=>$recipe->id));
    $tipMaxLen=RECIPE::TIP_MAX_LEN;
    $cs->registerScript('setTip', 
        "$('#recipe-tip').editable('$setTipUrl', {
            indicator : '$indicator',
            type      : 'textarea_charcounter',
            rows      : 2,
            charcounter : {
                characters : $tipMaxLen
            }            
        });"
    );    

    $setImageUrl=CHtml::normalizeUrl(array('setImage','id'=>$recipe->id));
    $cs->registerScript('setImage', "
        $('#recipe-image').after('<div><a id=\'edit-image\' href=\'\'>修改图片</a></div>');
        $('#edit-image').colorbox({href:'$setImageUrl',width:500, height:240, iframe:true});
        "
    );
    $cs->registerScript('functionSetImage',"
        function setRecipeImage(url)
        {
            $('#recipe-image').attr('src',url);
        }", CClientScript::POS_HEAD);
    
    $setIngredientsUrl=CJavaScript::quote(CHtml::normalizeUrl(array('setIngredients','id'=>$recipe->id)));
    $cs->registerScript('setIngredients', 
        "$('#recipe-ingredients').colorbox({href:'$setIngredientsUrl',width:500, height:300, iframe:true});
         $('#recipe-ingredients').attr('style','cursor: pointer;');
        "
    );
    $cs->registerScript('functionSetIngredients',"
        function setRecipeIngredients(content)
        {
            $('#recipe-ingredients').html(content);
        }", CClientScript::POS_HEAD);    
}
?>

<div id='recipe-wrapper'>

  <h2>菜谱 #<?php echo $recipe->id?></h2>
<?php
if($edit){
    if(!$recipe->isPublished()) {
?>
    <div><a href="javascript:publishThis();">发布</a></div>
    <script type="text/javascript">
        function publishThis()
        {
            if(!confirm('您确认发布此食谱？')){
                return;
            }
            
            var publishRecipeUrl='<?=CHtml::normalizeUrl(array('publish'))?>';
            $.post(publishRecipeUrl,
                {id : <?=$recipe->id?>},
                function(data){
                    if(data.succ){
                        alert('发布成功');
                        window.location=data.redirectUrl;
                    }
                    else {
                        if(data.error) alert(data.error);
                    }
                }, 'json');            
        }
    </script>
<?php
    }
?>
    <div><a href="javascript:deleteThis();">删除</a></div>
    <script type="text/javascript">
        function deleteThis()
        {
            if(!confirm('您确认删除此食谱？')){
                return;
            }
            var deleteRecipeUrl='<?=CHtml::normalizeUrl(array('delete'))?>';
            $.post(deleteRecipeUrl,
                {id : <?=$recipe->id?>},
                function(data){
                    if(data.succ){
                        alert('删除成功');
                        window.location=data.redirectUrl;
                    }
                    else {
                        if(data.error) alert(data.error);
                    }
                }, 'json');
        }
    </script>    
<?php
}
?>
  
  <div>
  厨师 <a href="<?=CHtml::normalizeUrl(array('kitchen/show', 'id'=>$recipe->userId))?>"><?=$recipe->userName?></a>
  </div>
  
  <div>
    <h3>菜名</h3>
    <div id='recipe-title'><?php echo CHtml::encode($recipe->title); ?></div>
  </div>

  <div>
    <h3>描述</h3>
    <p id='recipe-description'><?php echo nl2br(CHtml::encode($recipe->description)); ?></p>
  </div>

  <div>
    <h3>功效</h3>
    <p id='recipe-effectiveness'><?php echo nl2br(CHtml::encode($recipe->effectiveness)); ?></p>
  </div>
  
  <div>
    <h3>图片</h3>
    <div id='recipe-image-div'>
        <?php 
            $image=UserImage::toUrl('recipe', $recipe->image, 'big');
            echo CHtml::image($image,'',array('id'=>'recipe-image'));
        ?>
    </div>
  </div>  
  
  <div>
    <h3>配料</h3>
    <div id='recipe-ingredients'>
    <?php
        $this->renderPartial('_ingredients', array('recipe'=>$recipe));
    ?>
    </div>
  </div>  
  
  <div>
     <h3>烹饪方法</h3>
     <div id='recipe-steps'>
     <?php
        $this->renderPartial('/step/_steps', array('recipe'=>$recipe,'steps'=>$steps,'edit'=>$edit));
     ?>
     </div>
  </div>

  <div>
    <h3>小贴士</h3>
    <p id='recipe-tip'><?php echo nl2br(CHtml::encode($recipe->tip)); ?></p>
  </div>

</div>

<?php
if($recipe->isPublished()) {
    $this->renderPartial('/comment/ajaxList', array('comments'=>$comments, 'recipe'=>$recipe));
}
?>
